package com.lill.product.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.lill.common.component.model.P;
import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.product.model.ProductCarModel;
import com.lill.product.service.IProductCarService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.constant.SystemConstant;
import com.lill.system.model.SysUserEntity;

/**
 * 产品车辆信息Controller
 *
 * @author lill
 * @date 2021-08-27 09:14:44
 */
@RestController
@RequestMapping("product/car")
public class ProductCarController {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
    @Autowired
    private IProductCarService productCarService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	if(!user.getId().equals(SystemConstant.SUPER_ADMIN)){
    		params.put("companyId", user.getCompanyId());
    	}
        //查询列表数据
        Query query = new Query(params);
        List<ProductCarModel> productCarList = productCarService.queryList(query);
        int total = productCarService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(productCarList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
    	ProductCarModel productCar = productCarService.queryObject(id);
        return R.ok().put("productCar", productCar);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ProductCarModel productCar,@CurrentUser SysUserEntity user) {
    	productCar.setTopClassifyId(ProductCarModel.classify);
    	productCar.setCompanyId(user.getCompanyId());
    	productCar.setStatus(1);
    	productCar.setCreateUser(user.getId());
    	productCar.setCreateTime(new Date());
        productCar.setAllName(joinAllname(productCar));
        int result=productCarService.save(productCar);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ProductCarModel productCar,@CurrentUser SysUserEntity user) {
    	if(ProductCarModel.classify.equals(productCar.getId())){
    		return R.failure().put("msg", "系统权限不足");
    	}
    	productCar.setTopClassifyId(ProductCarModel.classify);
    	productCar.setCompanyId(user.getCompanyId());
    	productCar.setUpdateUser(user.getId());
    	productCar.setUpdateTime(new Date());
    	productCar.setAllName(joinAllname(productCar));
        int result=productCarService.update(productCar);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }
    
    @RequestMapping("/import")
    public R importData(@RequestParam MultipartFile file,@CurrentUser SysUserEntity user) {
    	P result=P.ok();
    	try {
			Map<String, Object> baseMap=new HashMap<String, Object>();
			baseMap.put("company_id", user.getCompanyId());
			baseMap.put("branch_id", user.getBranchId());
			baseMap.put("create_user", user.getId());
			baseMap.put("create_time", new Date());
			boolean xlsxFormat=false;
			if(file.getOriginalFilename().endsWith("xlsx")||file.getOriginalFilename().endsWith("XLSX")){
				xlsxFormat=true;
			}
			result=productCarService.importData(baseMap,"product_car",file.getInputStream(),xlsxFormat);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
    	if(result.isR()){
    		return R.ok();
    	}else{
    		return R.failure();
    	}
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	if(!user.getId().equals(SystemConstant.SUPER_ADMIN)){
    		params.put("companyId", user.getCompanyId());
    	}
        List<ProductCarModel> list = productCarService.queryList(params);
        return R.ok().put("list", list);
    }
    
    private String joinAllname(ProductCarModel productCar){
        StringBuffer allNameBuffer=new StringBuffer("");
        if(StringUtils.isNotEmpty(productCar.getCarSerie())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productCar.getCarSerie());
        }
        if(StringUtils.isNotEmpty(productCar.getCarModel())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productCar.getCarModel());
        }
        if(StringUtils.isNotEmpty(productCar.getDesign())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productCar.getDesign());
        }
        if(StringUtils.isNotEmpty(productCar.getConfig())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productCar.getConfig());
        }
        if(StringUtils.isNotEmpty(productCar.getColour())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productCar.getColour());
        }
        if(StringUtils.isNotEmpty(productCar.getSpeedBox())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productCar.getSpeedBox());
        }
        if(StringUtils.isNotEmpty(productCar.getDisplacement())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productCar.getDisplacement());
        }
        if(StringUtils.isNotEmpty(productCar.getPower())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productCar.getPower());
        }
        return allNameBuffer.toString();
    }
}
